package com.smy.jkds.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smy.jkds.po.*;
import com.smy.jkds.service.CardService;
import com.smy.jkds.vo.IdListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 卡片表 前端控制器
 * </p>
 *
 * @author 01140
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/jkds")
public class CardController {

    @Autowired
    CardService cardService;

    @PostMapping("/createCard")
    public Result createCard(@RequestBody Card card, @CurrentUser JkdsUser user){
        card.setUserId(user.getId());
        card.setUserName(user.getName());
        String content = card.getContent();
        int lastIndex = content.lastIndexOf("#");
        if (lastIndex > -1){
            card.setContent(content.substring(lastIndex + 1));
            int beginIndex = content.indexOf("#");
            card.setTag(content.substring(beginIndex+1, lastIndex));
        }
        boolean r = cardService.save(card);
        return r ? Result.success("创建成功") : Result.success("创建失败");
    }

    @PostMapping("/updateCard")
    public Result updateCard(@RequestBody Card card, @CurrentUser JkdsUser user){
        boolean r = cardService.updateById(card);
        return r ? Result.success("更新成功") : Result.success("更新失败");
    }

    @PostMapping("/deleteCard")
    public Result deleteCard(@RequestBody IdListVO cardDeleteVO, @CurrentUser JkdsUser user){
        if (cardDeleteVO == null || cardDeleteVO.getIdList() == null){
            return Result.success("更新成功");
        }
//        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in(Card.ColumnEnum.id.name(), cardDeleteVO.getIdList());
        boolean r = cardService.removeByIds(cardDeleteVO.getIdList());
        return r ? Result.success("删除成功") : Result.success("删除失败");
    }

    @GetMapping("/queryCardList")
    public Result queryTaskList(@CurrentUser JkdsUser loginUser, String taskId, String createDate){
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Card.ColumnEnum.task_id.name(), taskId);
        return Result.success("查询成功",cardService.list(queryWrapper));
    }
}

